package com.hnfnu.controller;

import com.hnfnu.constant.DonationsTradeNo;
import com.hnfnu.entity.Donations;
import com.hnfnu.entity.Link;
import com.hnfnu.protocol.dto.ResponseResult;
import com.hnfnu.protocol.model.bo.DonationRecordAddRecordInputBO;
///import com.hnfnu.service.DonationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@Controller
@RequestMapping("/alipay")
public class AlipayController extends BaseController{

//    @Autowired
//    private DonationRecordService donationRecordService;
    @PostMapping("/payNotify")
    public ResponseResult payNotify() {
        String tradeNo = DonationsTradeNo.getTradeNo();
        Donations donations = donationsService.getByTrade(tradeNo);
        // 捐款记录上链
        DonationRecordAddRecordInputBO inputBO = new DonationRecordAddRecordInputBO();
        inputBO.setMoney(donations.getAmount().toString());
        inputBO.setRemark(donations.getMessage());
        inputBO.setUserId(BigInteger.valueOf(DonationsTradeNo.getUserId()));
        inputBO.setProjectId(BigInteger.valueOf(donations.getProjectId()));
        inputBO.setRecordTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        try {
            //donationRecordService.addRecord(inputBO);
        } catch (Exception e) {
            return new ResponseResult(500,"捐款失败");
        }
        donationsService.updateStatus(tradeNo);
        return new ResponseResult(200, "捐款成功");
    }
}
